package com.example.secondhandmall.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.example.secondhandmall.R;
import com.example.secondhandmall.api.Api;
import com.example.secondhandmall.api.ApiConfig;
import com.example.secondhandmall.api.MyCallback;
import com.example.secondhandmall.entity.LoginResponse;
import com.example.secondhandmall.entity.PwdSwitch;
import com.example.secondhandmall.util.StringUtils;
import com.google.gson.Gson;

import java.util.HashMap;

public class LoginActivity extends BaseActivity {

    private EditText etAccount;
    private CheckBox cbRememberPwd;
    private Button btnLogin;

    private PwdSwitch pwd1;

    String spFileName = "sp_login";
    String accountKey = "login_account";
    String pwdKey = "login_pwd";
    String rememberPwdKey = "login_remember_pwd";

    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        etAccount = findViewById(R.id.et_account);
        pwd1 = new PwdSwitch(findViewById(R.id.et_pwd), findViewById(R.id.iv_pwd_switch));
        cbRememberPwd = findViewById(R.id.cb_remember_pwd);
        btnLogin = findViewById(R.id.btn_login);
    }

    @Override
    protected void initData() {
        init();

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = etAccount.getText().toString().trim();
                String pwd = pwd1.etPwd.getText().toString().trim();
                login(account, pwd);
            }
        });
    }

    //初始化
    private void init() {
        SharedPreferences spFile = getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        String account = spFile.getString(accountKey, null);
        String pwd = spFile.getString(pwdKey, null);
        Boolean rememberPwd = spFile.getBoolean(rememberPwdKey, false);

        if (!StringUtils.isEmpty(account)) {
            etAccount.setText(account);
        }
        if (!StringUtils.isEmpty(pwd)) {
            pwd1.etPwd.setText(pwd);
        }

        cbRememberPwd.setChecked(rememberPwd);
    }

    //登录
    private void login(String account, String pwd) {
        if (StringUtils.isEmpty(account)) {
            showToast("请输入账号!");
            return;
        }
        if (StringUtils.isEmpty(pwd)) {
            showToast("请输入密码!");
            return;
        }

        //记住密码复选框
        SharedPreferences spFile = getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = spFile.edit();

        if (cbRememberPwd.isChecked()) {
            editor.putString(accountKey, account);
            editor.putString(pwdKey, pwd);
            editor.putBoolean(rememberPwdKey, true);
            editor.apply();
        } else {
//            editor.remove(accountKey);
            editor.putString(accountKey, account);
            editor.remove(pwdKey);
            editor.remove(rememberPwdKey);
            editor.apply();
        }

        //没接口直接登录
//        saveStringToSp("account",account);
//        navigateTo(HomeActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);

        //网络请求
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("userId", account);
        params.put("passWord", pwd);
        Api.config(ApiConfig.LOGIN, params).postRequest(new MyCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess",res);

                //把json字符串转转换为实体对象
                Gson gson=new Gson();
                LoginResponse loginResponse = gson.fromJson(res, LoginResponse.class);
                if(loginResponse.getCode()==200){
//                    SharedPreferences sp  = getSharedPreferences("sp_account",Context.MODE_PRIVATE);
//                    SharedPreferences.Editor editor = sp.edit();
//                    editor.putString("account",account);
//                    editor.putString("token",token);
//                    editor.commit();
                    if(loginResponse.isData()){
                    saveStringToSp("account",account);
                    navigateTo(HomeActivity.class,Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                    showToastSync("登陆成功！");}else{
                        showToastSync("账号或密码错误，请重新登录！");
                    }
                }else{
                    showToastSync("登录失败！");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure",e.toString());
//                });
                showToastSync("请求失败！");
            }
        });
    }
}